import {
  get,
  post,
} from "./base_service"

/**
 * 新的IM登录
 */
const newImLogin = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/user/login", data, true).then((data) => {
      if (data.code == 200) {
        resolve(data)
        wx.setStorageSync("imToken", data.data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 注册
 * @param {*} data 
 */
const newImRegister = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/user/register", data, true).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 获取聊天会话列表
 * @param {*} data 
 */
const queryIMConversationList = () => {
  return new Promise((resolve, reject) => {
    post("/api/message/conversation/list", {}, true).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 获取对应消息列表
 * @param {*} data 
 */
const queryIMMessageList = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/message/list", data, true).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 已读消息设置
 * @param {*} data 
 */
const setMessageIsRead = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/message/read", data, true).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 删除一个会话列表
 * @param {*} data 
 */
const cancelConversationRequest = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/message/conversation/del", data, true).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 更新用户IM信息
 * @param {*} data 
 */
const updateUserImInfo = (data) => {
  return new Promise((resolve, reject) => {
    post("/api/user/update/info", data, true).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 根据openId获取对方的im信息
 */
const getOtherImInfo = (account) => {
  return new Promise((resolve, reject) => {
    post("/api/user/query/info?account=" + account, {}, true).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}




module.exports = {
  newImLogin,
  newImRegister,
  queryIMConversationList,
  queryIMMessageList,
  setMessageIsRead,
  cancelConversationRequest,
  updateUserImInfo,
  getOtherImInfo
}